package com.by.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.bean.Model;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.group.ISave;
import com.by.common.group.IUpdate;
import com.by.common.query.ModelQuery;
import com.by.service.ModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@Api(tags = "产品型号")
@RequestMapping("/v1")
public class ModelController extends BaseController {
    @Autowired
    ModelService modelService;

//    @ApiOperation("查询所有产品型号")
//    @GetMapping("/model/all")
//    public ResultBean queryAll(){
//        List list = ModelService.queryAll();
//        return success("产品型号",list);
//    }

    @ApiOperation("产品型号列表")
    @PostMapping("/model/query")
    public ResultBean query(@RequestBody ModelQuery query){
        Map map = BeanUtil.beanToMap(query);
        PageBean bean = modelService.query(map);
        return success("产品型号列表",bean);
    }


    @ApiOperation("保存产品型号")
    @PostMapping("/model")
    public ResultBean save(@Validated(ISave.class) @RequestBody Model model, BindingResult result){
        validate(result);
        //检查用户名是否存在
        int rs=modelService.save(model);
        return saveResult(rs);
    }

    @ApiOperation("删除产品型号")
    @DeleteMapping("/model")
    public ResultBean deleteEmployee(@ApiParam(name = "id", value = "要删除的用户id") @RequestParam(name = "id", required = true, defaultValue = "0") int id) {
        int rs = modelService.delete(id);
        return deleteResult(rs);
    }

    @ApiOperation("修改产品型号")
    @PutMapping("/model")
    public ResultBean update(@Validated(IUpdate.class) @RequestBody Model model, BindingResult result){
        validate(result);
        int rs = modelService.update(model);
        return updateResult(rs);
    }
}
